package com.controller;

import com.github.pagehelper.PageInfo;
import com.pojo.Address;
import com.pojo.R;
import com.pojo.User;
import com.service.AddressService;
import com.service.UserService;
import com.util.Emailcode;
import com.util.UUIDutil;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private AddressService addressService;
    /**
     * 登录校验
     * @param account
     * @param password
     * @return
     */
    @RequestMapping("/login")
    public Object login(String account,String password){
        User login = userService.login(account, password);
        if (login!=null){
            return new R("登录成功","200",login);
        }else{
            return  new R<>("账号或密码错误","400",null);
        }
    }

    /**
     * 查询所有用户信息
     * @param pageNum
     * @param authtatus
     * @return 将结果封装到R对象中并返回
     */
    @RequestMapping("/selectAll")
    public Object selectAll(@RequestParam(defaultValue = "1")String pageNum,@RequestParam(defaultValue = "")String authtatus){
        PageInfo<User> userPageInfo = userService.selectAll(Integer.parseInt(pageNum), authtatus);
        if (userPageInfo!=null){
            return new R("","200",userPageInfo);
        }else{
            return  new R<>("没有用户","400",null);
        }
    }

    /**
     * 根据id来展示用户信息
     * @param id
     * @return
     */
    @RequestMapping("/selectById")
    public Object selectById(long id){
        User user = userService.selectById(id);
        if (user!=null){
            return new R("","200",user);
        }else{
            return  new R<>("没有用户","400",null);
        }
    }

    /**
     * 更新用户信息
     * @param user
     * @return 将结果以R对象返回
     */
    @RequestMapping("/updateUser")
    public Object updateUser(User user){
        int i = userService.updateUser(user);
        R r = new R();
        if (i==1){
            r.setCode("200");
            r.setMess("更新成功");
        }else{
            r.setCode("400");
            r.setMess("更新成功");
        }
       return r;
    }

    /**
     * 验证用户账号的唯一性
     * @param account
     * @return
     */
    @RequestMapping("/selectByAccount")
    public Object selectByAccount(String account){
        User user = userService.selectByAccount(account);
        if (user!=null){
            return new R("该账号已存在","400",user);
        }else{
            return  new R<>("","200",null);
        }
    }

    /**
     * 后台用到的插入信息 前台注册
     * @param user
     * @return
     */
    @RequestMapping("/insertUser")
    public Object insertUser(User user){
        int i = userService.insertUser(user);
        R r = new R();
        if (i==1){
            r.setCode("200");
            r.setMess("添加成功");
        }else{
            r.setCode("400");
            r.setMess("添加失败");
        }
        return r;
    }
    @RequestMapping("/deleteUser")
    public Object deleteUser(long id){
        int i = userService.deleteUser(id);
        R r = new R();
        if (i==1){
            r.setCode("200");
            r.setMess("删除成功");
        }else{
            r.setCode("400");
            r.setMess("删除失败");
        }
        return r;
    }
    //资质图片预览
    @RequestMapping("/vueylupload")
    public void vueylupload(String filename, HttpServletResponse response) throws Exception{
        File file = new File("e:/img",filename);
        /*读取头像*/
        FileInputStream fis = new FileInputStream(file);
        response.setHeader("Content-Disposition","filename="+filename);
        ServletOutputStream os = response.getOutputStream();
        IOUtils.copy(fis,os);
    }
    /*资质图片上传*/
    @RequestMapping("/vueupload")
    public Object vueupload( MultipartFile file) throws Exception{
        String filename = file.getOriginalFilename();
        if (filename.contains("/")){
            filename = filename.substring(filename.lastIndexOf("/")+1);
        }
        filename = UUIDutil.uuid()+filename;
        file.transferTo(new File("e:/img/2021-04",filename));

        R r = new R();
        r.setMess("2021-04/"+filename);//返回的图片名称
        r.setCode("200");
        return r;
    }

    /**
     * 资质认证步骤，更新认证信息
     * @param user
     * @return
     */
    @RequestMapping("/zzupdate")
    public Object zzupdate(User user){
        System.out.println(user.toString());
        int i = userService.zzupdate(user);
        R r = new R();
        if (i==1){
            r.setCode("200");
            r.setMess("认证中，请耐心等待");
        }else{
            r.setCode("400");
            r.setMess("认证失败");
        }
        return r;
    }
    //更新用户认证状态
    @RequestMapping("/updateAuthtatus")
    public Object updateAuthtatus(long id,long authtatus){
        int i = userService.updateAuthtatus(id, authtatus);
        R r = new R();
        if (i==1){
            r.setCode("200");
            r.setMess("更新成功");
        }else{
            r.setCode("400");
            r.setMess("更新失败");
        }
        return r;
    }

    /**
     *根据用户id来查询改用户下的所有地址
     * @param userId 用户id
     * @return 返回一个addresses对象集合
     */
    @RequestMapping("/selectAllAddress")
    public Object selectAllAddress(long userId){
        List<Address> addresses = addressService.selectByuserId(userId);
        return addresses;
    }
    /**
     * 传入新地址，并将结果返回
     * @param address 传入的地址信息
     * @return 返回R对象
     */
    @RequestMapping("/insertAddress")
    public Object insertAddress(Address address){
        int i = addressService.insert(address);
        R r = new R();
        if (i==1){
            r.setCode("200");
            r.setMess("添加成功");
            r.setT(address);
        }else{
            r.setCode("400");
            r.setMess("添加失败");
        }
        return r;
    }

    /**
     * 读取文件
     * @param filename
     * @param response
     * @throws Exception
     */
    @RequestMapping("/download")
    public void download(String filename,HttpServletResponse response) throws Exception{
        File file =new File("d:/img",filename);
        FileInputStream fis = new FileInputStream(file);
        response.setHeader("Content-Disposition","filename="+filename);
        ServletOutputStream os = response.getOutputStream();
        IOUtils.copy(fis,os);
    }

    /**
     * 文件上传
     * @param file
     * @return
     * @throws Exception
     */
    @RequestMapping("/upload")
    public  Object upload(MultipartFile file) throws Exception{
        String filename = file.getOriginalFilename();
        if (filename.contains("/")){
            filename = filename.substring(filename.lastIndexOf("/")+1);
        }
        filename = UUIDutil.uuid()+filename;
        file.transferTo(new File("d:/img/2021-03",filename));
        R r= new R("2021-03/"+filename,"200",null);
        return r;
    }
    @RequestMapping("/sendCode")
    public Object sendCode(String email){
        String s = Emailcode.sendEamilCode(email);
        return s;
    }
    @RequestMapping("/vuequeryimg")
    public void queryimg(String touxiang, HttpServletResponse response){
        File file = new File("d:/img/",touxiang);
        FileInputStream fin = null;
        try {
            fin = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        // 设置响应头信息
        response.setHeader("Content-Disposition", "filename="+touxiang);//预览
        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 拷贝
        try {
            IOUtils.copy(fin,out);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
